<?php

use app\library\exception\ApiException;
use app\library\facade\Config;
use app\library\ErrorCode;

/**
 * Class Base
 */
class BaseController extends \Yaf\Controller_Abstract
{
    use \app\library\traits\Json;

    protected $user_id = 0;

    public function init()
    {
        $this->checkSign();
    }

    /**
     * 检测是否登录
     * @throws ApiException
     */
    protected function checkLogin()
    {

        if (Config::get('app.debug') && $this->getRequest()->getQuery('test')) {
            $this->user_id = $this->getRequest()->getQuery('test');
            return true;
        }
        $key = $this->getRequest()->getServer('HJSC-K');
        $user_id = $this->getRequest()->getServer('HJSC-U');
        if (empty($key) || empty($user_id)) {
            throw new ApiException('需要登录', 10001);
        }
        //这里可以改成从缓存中读取
        $user_info = (new UserBaseModel())->find($user_id);
        if ($user_info) {
            $txt = base64_decode($key);
            $encrypt_key = md5($user_info['user_key']);
            $ctr = 0;
            $tmp = '';
            $txt_len = strlen($txt);
            $encrypt_key_len = strlen($encrypt_key);
            for ($i = 0; $i < $txt_len; $i++) {
                $ctr = $ctr == $encrypt_key_len ? 0 : $ctr;
                $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
            }
            $txt = $tmp;
            $tmp = '';
            $txt_len = strlen($txt);
            for ($i = 0; $i < $txt_len; $i++) {
                $tmp .= $txt[$i] ^ $txt[++$i];
            }
            $decrypt_str = $tmp;
            parse_str($decrypt_str, $user_row);
            if ($user_row['user_id'] != $user_id) {
                throw new ApiException('登录失败', 10001);
            }
            $this->user_id = $user_id;
        } else {
            throw new ApiException('需要登录哦', 10001);
        }
    }

    /**
     * 签名校验
     */
    protected function checkSign()
    {
        $request = $this->getRequest();
        if (Config::get('app.debug') && $request->getQuery('test')) {
            return true;
        }
        $time = $request->getServer('TIME');
        $notify = $request->getServer('NOTIFY');
        $sign = $request->getServer('SIGN');

        if (empty($notify) || empty($time) || empty($sign)) {
            throw new ApiException('缺少必要参数', 10002);
        }
        //签名有效期三分钟
        if ($time < (time() - 180)) {
            throw new ApiException('请求超时', 10003);
        }
        $u = $request->getServer('HJSC-U', 'common');
        if ($u == '') $u = 'common';
        $key = 'YKmqN9ypFneJIBGL';
        $sign_new = strtoupper(md5($u . $time . $notify . $key));
        if ($sign != $sign_new) {
            throw new ApiException('签名错误', 10004);
        }
    }

    /**
     * 缺少参数
     */
    protected function noParams($message = null){
        return $this->returnError(ErrorCode::NO_PARAMS[0], $message ?? ErrorCode::NO_PARAMS[1]);
    }


}